package com.shequan.search.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.shequan.activity.bean.pojo.ActivityTag;
import com.shequan.search.bean.bo.QryActivityDetailBean;
import com.shequan.search.bean.bo.QryActivityParamBean;
import com.shequan.search.bean.bo.QryShequanParamBean;
import com.shequan.search.bean.bo.SearchActivityBean;
import com.shequan.search.bean.bo.SearchUserInfoBean;
import com.shequan.search.bean.bo.UserDetailBean;
import com.shequan.search.dao.SearchDAO;
import com.shequan.search.service.SearchService;
import com.shequan.shequan.bean.pojo.Shequan;

/**
 * <P>
 * Copyright 2012 shequaner. 搜索
 * 
 * All Rights Reserved.
 * 
 * @author mazg
 * @version 1.0, 2012-3-1
 * @see com.shequan.search.service.impl.SearchServiceImpl
 * @since 1.0
 */
public class SearchServiceImpl implements SearchService {
	private static final Logger log = Logger.getLogger(SearchServiceImpl.class);
	private SearchDAO searchDAO;

	/**
	 * 查询社员信息
	 */
	public List<SearchUserInfoBean> searchFriendList(String userName, String areaName, String schoolName, Integer userGender) throws Exception {
		List<SearchUserInfoBean> userInfoList = null;
		if (userName == null && areaName == null && schoolName == null && userGender == null) {
			userInfoList = null;
		} else {
			UserDetailBean userDetail = new UserDetailBean();
			if (userName != null && !"".equals(userName)) {
				userDetail.setUserName("%" + userName + "%");
			}
			userDetail.setSchoolName(schoolName);
			userDetail.setAreaName(areaName);
			userDetail.setUserGender(userGender);
			List<UserDetailBean> userDetailList = searchDAO.queryUserInfoListByUserDetail(userDetail);
			for (int i = 0; userDetailList != null && i < userDetailList.size(); i++) {
				SearchUserInfoBean userInfo = new SearchUserInfoBean();
				userInfo = (SearchUserInfoBean) userDetailList.get(i);
				List<Shequan> shequanList = searchDAO.queryShequanListByUserId(userInfo.getUserId());
				userInfo.setShequanList(shequanList);
				userDetailList.add(userInfo);
			}
		}
		return userInfoList;
	}

	public List<Shequan> searchShequanList(String shequanName, String conUserName, String schoolName, Date startDate, Date endDate) throws Exception {
		List<Shequan> shequanList;
		if (shequanName != null && conUserName == null && schoolName == null && startDate == null && endDate == null) {
			shequanList = null;
		} else {
			QryShequanParamBean shequanParamBean = new QryShequanParamBean();
			if (shequanName != null && !"".equals(shequanName)) {
				shequanName = "%" + shequanName + "%";
				shequanParamBean.setShequanName(shequanName);
			}
			if (conUserName != null && !"".equals(conUserName)) {
				conUserName = "%" + conUserName + "%";
				shequanParamBean.setConUserName(conUserName);
			}
			shequanParamBean.setSchoolName(schoolName);
			shequanParamBean.setStartDate(startDate);
			shequanParamBean.setEndDate(endDate);
			shequanList = searchDAO.queryShequanListByParamBean(shequanParamBean);
		}
		return shequanList;
	}

	public List<SearchActivityBean> queryActivityList(String activityName, String shequanName, Date startTime, String activityPlace, String schoolName) throws Exception {
		List<SearchActivityBean> searchActivityList;
		if (activityName == null && shequanName == null && startTime == null && activityPlace == null && schoolName == null) {
			searchActivityList = null;
		} else {
			QryActivityParamBean activityParamBean = new QryActivityParamBean();
			if (activityName != null && !"".equals(activityName)) {
				activityParamBean.setActivityName("%" + activityName + "%");
			}
			if (shequanName != null && !"".equals(shequanName)) {
				activityParamBean.setShequanName("%" + shequanName + "%");
			}
			if (activityPlace != null && !"".equals(activityPlace)) {
				activityParamBean.setActivityPlace("%" + activityPlace + "%");
			}
			if (schoolName != null && !"".equals(schoolName)) {
				activityParamBean.setSchoolName("%" + schoolName + "%");
			}
			if (startTime != null) {
				activityParamBean.setStartTime(startTime);
			}
			List<QryActivityDetailBean> activityDetailList = searchDAO.queryActivityDetailListByParamBean(activityParamBean);
			searchActivityList = new ArrayList<SearchActivityBean>();
			if (activityDetailList != null && activityDetailList.size() > 0) {
				for (int i = 0; i < activityDetailList.size(); i++) {
					SearchActivityBean activityBean = (SearchActivityBean) activityDetailList.get(i);
					List<ActivityTag> activityTagList = searchDAO.queryActivitTagListByActId(activityBean.getActivityId());
					activityBean.setActivityTagList(activityTagList);
				}
			}
		}
		return searchActivityList;
	}

	/**
	 * @param searchDAO
	 *            the searchDAO to set
	 */
	public void setSearchDAO(SearchDAO searchDAO) {
		this.searchDAO = searchDAO;
	}

}
